package com.xteam.xnetworkdisk.sys.controller;

import com.xteam.xnetworkdisk.common.CurrentUser;
import com.xteam.xnetworkdisk.constant.Constant;
import com.xteam.xnetworkdisk.constant.FIleTypeConstant;
import com.xteam.xnetworkdisk.sys.dao.FileDao;
import com.xteam.xnetworkdisk.sys.dao.UserDao;
import com.xteam.xnetworkdisk.sys.entity.FileEntity;
import com.xteam.xnetworkdisk.sys.service.FileService;
import com.xteam.xnetworkdisk.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

@RestController
@RequestMapping()
public class UploadFileController {
    @Resource
    private FileDao fileDao;

//    @Autowired
//    private MinioUtilsNew minioUtils;

//    @Value("${minio.endpoint}")
//    private String endpoint;


    /**
     * 普通文件上传
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public R upload(@RequestParam("file") MultipartFile file, @RequestParam("md5") String md5Value,
                    @RequestParam("folderId") Integer folderId,@RequestParam("userid") Integer userid) {
        upload1(file, md5Value, folderId,userid);
        // 这里实现文件分片上传的逻辑
        return R.ok();
    }


    public R upload1(MultipartFile file, String md5Value, Integer folderId,Integer userid) {
//        //获取文件相应的信息
//        String fileName = file.getOriginalFilename();
//        Long fileSize = file.getSize();
//        String contentType = file.getContentType();
//        String bucketName = Constant.USER_FILE_BUKET;
//
//        //通过contentType得到它在数据库中的类型所代表的id
//        int fileType = FileTypeUtils.getFileType(contentType);
//
//        try {
//
//            String originalFilename = file.getOriginalFilename();
//            String suffix = Tools.getSuffix(originalFilename);
//
//            //上传文件到minio
//            String videoFileName = minioUtils.uploadFile(file, bucketName,suffix);
//
//            //在数据库生成相应的信息
//            FileEntity fileEntity = new FileEntity();
//            fileEntity.setFileName(fileName);
//            fileEntity.setFilePath(endpoint + "/" + bucketName + "/" + videoFileName);
//            fileEntity.setFileSize(fileSize);
//            fileEntity.setCreatedAt(new Date());
//            fileEntity.setIsFolder(false);
//            fileEntity.setIsValid(true);
//            fileEntity.setFileKey(md5Value);
//            fileEntity.setFileType(fileType);
//            fileEntity.setFolderId(folderId);
//            fileEntity.setUserId(userid);
//            //fileEntity.setUserId(1785409564);
//
//            //新增file记录
//            fileDao.insertFile(fileEntity);
//
//            //类型是视频类型
//            if (fileType == FIleTypeConstant.VIDEO) {
//
//                try (OutputStream thumbnailStream = new ByteArrayOutputStream()) {
//                    // 生成封面图像
//                    VideoUtil.getVideoImage(file, thumbnailStream);
//
//                    // 上传封面图像到 MinIO
//                    String thumbnailFileName = videoFileName + Constant.VIDEO_SUFFIX;
//                    InputStream thumbnailInputStream = new ByteArrayInputStream(((ByteArrayOutputStream) thumbnailStream).toByteArray());
//                    minioUtils.uploadFile(thumbnailInputStream, bucketName, thumbnailFileName,Constant.VIDEO_INDEX_IMAGE_TPYE);
//                }
//            }
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
        return R.ok();
    }
}
